import Game from "../base/Game";
import GameEvent from "../event/GameEvent";
import BookEvent_Emit from "../event/BookEvent_Emit";
import GameDragonEffect from "../playerScript/GameDragonEffect";

const {ccclass, property} = cc._decorator;

@ccclass
export default class BattleScene extends cc.Component {

 
    @property(cc.Button)
    private closeBtn:cc.Button = null;

    @property(cc.Node)
    private battleTopIBg:cc.Node = null;
    @property(cc.Node)
    private battleTopIIBg:cc.Node = null;
    @property(cc.Node)
    private battleBottomIBg:cc.Node = null;
    @property(cc.Node)
    private battleBottomIIBg:cc.Node = null;

    @property(cc.Node)
    private dragonBonesNode:cc.Node = null;
    @property(cc.Node)
    private db1Node:cc.Node = null;
    @property(cc.Node)
    private db2Node:cc.Node = null;
    @property(cc.Node)
    private db3Node:cc.Node = null;

    onLoad () {
    }

    start () {
        this.closeBtn.node.on(cc.Node.EventType.TOUCH_END,this.clean,this);
        GameEvent.ins.addEvent(BookEvent_Emit.PLAY_BULLET_EVENT,this.playBulletFunc,this);
        this.playBulletFunc(null);
    }

    private count :number = 0;
    update (dt) {
        
        this.battleTopIBg.x -= 0.5;
        this.battleTopIIBg.x -= 0.5;
        if(this.battleTopIBg.x <= -720)
        {
            this.battleTopIBg.x = 720 + (this.battleTopIBg.x + 720);
        }
        if(this.battleTopIIBg.x <= -720)
        {
            this.battleTopIIBg.x = 720 + (this.battleTopIIBg.x + 720);
        }

        this.battleBottomIBg.x -= 2;
        this.battleBottomIIBg.x -= 2;
        if(this.battleBottomIBg.x <= -720)
        {
            this.battleBottomIBg.x = 720 + (this.battleBottomIBg.x + 720);
        }
        if(this.battleBottomIIBg.x <= -720)
        {
            this.battleBottomIIBg.x = 720 + (this.battleBottomIIBg.x + 720);
        }

    }

    private playBulletFunc(_node):void
    {
        //cc.warn("e",e);
        if(!_node) return
        if(_node.name == "dbNode")
        {
            this.bulletFunc(this.dragonBonesNode,this.db1Node);
        }
        else if(_node._name == "db1Node")
        {
            this.bulletFunc(this.db1Node,this.dragonBonesNode);
        }
        else if(_node._name == "db2Node")
        {
            this.bulletFunc(this.db2Node,this.dragonBonesNode);
        }   
        else if(_node._name == "db3Node")
        {
            this.bulletFunc(this.db3Node,this.dragonBonesNode);
        }
    }

    private bulletFunc(_self:any,_target:any):void
    {
        let bulletNode:cc.Node = new cc.Node()
        this.node.addChild(bulletNode);
        bulletNode.x = _self.x;
        bulletNode.y = _self.y;

        var effName = "B_meiguihua_1";
        if(_self != this.dragonBonesNode)
        {
            effName = "B_nor_1"
        }

        var db:dragonBones.ArmatureDisplay = bulletNode.addComponent(dragonBones.ArmatureDisplay);
        GameDragonEffect.loadDragonBones(db,effName,"Armature","play",this.playCompleteFunc);

        cc.tween(bulletNode)
                .to(0.2, {x:_target.x,y:_target.y},{ easing: 'sineOut'})
                .call(() => {
                     bulletNode.destroy();
                     this.playBulletBlastFunc(_self,_target);
                     })
                .start()
    }

	/** 子弹爆炸 */
	private playBulletBlastFunc(_self:any,_target:any):void
	{	

		let bulletNode:cc.Node = new cc.Node()
        this.node.addChild(bulletNode);
        //cc.director.getScene().addChild(bulletNode);
        bulletNode.x = _target.x;
        bulletNode.y = _target.y;

        var effName = "S_meiguihua_1";
        if(_self != this.dragonBonesNode)
        {
            effName = "B_electricBullets_02"
        }

        var db:dragonBones.ArmatureDisplay = bulletNode.addComponent(dragonBones.ArmatureDisplay);
        GameDragonEffect.loadDragonBones(db,effName,"Armature","play",this.playCompleteFunc);

        cc.tween(bulletNode)
        .to(0.5, {opacity :0},{ easing: 'sineOut'})
        .call(() => {
             bulletNode.destroy();
             })
        .start()
	}

    private playCompleteFunc():void
    {

    }

    private clean():void
    {   
        this.closeBtn.node.off(cc.Node.EventType.TOUCH_END,this.clean,this); 
        //this.node.destroy();
        cc.director.loadScene('MainScene');
    }
}
